package com.hins.demo.model.transfer.entity;

import com.hins.component.exception.Assert;
import com.hins.demo.model.transfer.valueobject.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * Description: 账户
 *
 * @author wenjiaqi
 * @date 2021/5/14 19:08
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    private AccountId accountId;
    private AccountNumber accountNumber;
    private UserId userId;
    private Money available;
    private Money dailyLimit;
    private Currency currency;

    public Currency getCurrency() {
        return this.available.getCurrency();
    }

    // 转入
    public void deposit(Money money) {
        Assert.isTrue(this.getCurrency().equals(money.getCurrency()));

        this.available = this.available.add(money);
    }

    // 转出
    public void withdraw(Money money) {
        Assert.isTrue(this.available.compareTo(money) >= 0);
        Assert.isTrue(this.dailyLimit.compareTo(money) >= 0);

        this.available = this.available.subtract(money);
    }
}
